//
//  LGTRWZOrderListVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/20.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTRWZOrderListVC: LGTBaseVC, UITableViewDelegate, UITableViewDataSource{

    var orderType:Int = 0
    var tableView:UITableView?
    var tableFrame:CGRect? = CGRect.zero
    var page:Int = 1
    var data:Array<TaskOrderModel> = Array.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setBlock()
    }
    
    func setBlock(){
        weak var weakSelf = self
        self.reloadBlock = {() in
            weakSelf?.tableView?.header?.beginRefreshing()
        }
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: true, title: "订单", back: false)
        
        let w = self.view.frame.width
        let h = self.view.frame.height
        self.tableView = UITableView.init(frame:  CGRect.init(x: 0.0, y: 0, width: w, height: h), style: UITableView.Style.plain)
        if self.tableFrame == CGRect.zero{
            self.tableView?.frame  = self.view.bounds
        }else{
            self.tableView?.frame  = self.tableFrame ?? CGRect.zero
        }
        
        self.view.addSubview(self.tableView!)
        self.tableView?.backgroundColor = ViewBackColor
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.estimatedRowHeight = 50
        self.tableView?.rowHeight = UITableView.automaticDimension
        self.tableView?.register(LGTRWZOrderViewCell.classForCoder(), forCellReuseIdentifier: "cell")
        
        weak var weakSelf = self
        let header = ZHRefreshNormalHeader.headerWithRefreshing(block: {
            weakSelf?.page = 1
            weakSelf?.getData()
        })
        self.tableView?.header = header
        
        
        self.tableView?.footer = ZHRefreshBackNormalFooter.footerWithRefreshing(block: {
            weakSelf?.page += 1
            weakSelf?.getData()
        })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTRWZOrderViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTRWZOrderViewCell
        cell.orderModel = self.data[indexPath.row]
        return cell
    }
    
    public func updateFrame(frame:CGRect){
        self.view.frame = frame
        self.tableFrame = self.view.bounds
        self.tableView?.frame = self.tableFrame!
    }
    
    func getData(){
        weak var weakSelf = self
        let param:Dictionary<String, Any> = ["type": weakSelf!.orderType, "page": weakSelf!.page]
        LGTHttpUtil.httpUtil.requestData(url: Task_OrderList_URL, param: param, showMessage: false, success: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
            let data:Array<Dictionary<String, Any>> =  json["data"] as? Array<Dictionary<String, Any>> ?? Array.init()
            var modeArray:Array<TaskOrderModel> = Array.init()
            for i in 0..<data.count{
                let param:Dictionary<String, Any> = data[i]
                let model:TaskOrderModel = try! lgtDecode(TaskOrderModel.self, param: param)
                modeArray.append(model)
            }
            
            if weakSelf?.page == 1{
                weakSelf?.data = modeArray
            }else{
                if weakSelf?.data == nil{
                    weakSelf?.data = Array.init()
                }
                weakSelf?.data.append(contentsOf: modeArray)
            }
            
            if weakSelf?.data.count == 0{
                weakSelf?.showNoData(image: NoData_Image, text: NoData_Text, subText: "", haveNav:false)
            }
            
            if modeArray.count == 0{
                weakSelf?.tableView?.footer?.endRefreshingWithNoMoreData()
            }else{
                weakSelf?.tableView?.footer?.resetNoMoreData()
            }
            weakSelf?.tableView?.reloadData()
        }, failure: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
        })
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        if self.data.count == 0{
//            self.tableView?.header?.beginRefreshing()
//        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailVC:LGTPostDetailVC = LGTPostDetailVC()
        detailVC.viewDetailType = .RWZ_OrderView_Type
        detailVC.orderModel = self.data[indexPath.row]
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    func reloadData(){
        self.noView?.isHidden = true
        self.tableView?.header?.beginRefreshing()
    }
}
